package com.hua.demo04;

import com.hua.demo01.TestThread2;
import org.apache.commons.io.FileUtils;

import java.io.File;
import java.io.IOException;
import java.net.URL;
import java.util.concurrent.*;

//线程创建方式三：
public class TestCallable implements Callable<Boolean> {
    private String name;
    private String url;

    public TestCallable(String name,String url){
        this.name = name;
        this.url = url;
    }






    @Override
    public Boolean call() throws Exception {
        WebDownLoader webDownLoader = new WebDownLoader();
        webDownLoader.downloader(url,name);
        System.out.println("下载文件名为："+name);
        return true;
    }


    public static void main(String[] args) throws ExecutionException, InterruptedException {
        TestCallable callable = new TestCallable("http://sc.cltt.org/Web/common/GeneratePhotoByStuID.ashx?StuID=5101621120079", "1.jpg");

        ExecutorService ser = Executors.newFixedThreadPool(1);

        Future<Boolean> r1 = ser.submit(callable);

        boolean rs1 = r1.get();
        ser.shutdownNow();






    }
}


//下载器
class WebDownLoader {

    public void downloader(String url, String name) {
        try {
            FileUtils.copyURLToFile(new URL(url),
                    new File(name));
        } catch (IOException e) {
            e.printStackTrace();
            System.out.println("IO异常,downloader方法出现问题");
        }

    }
}
/*
可以定义返回值
可以抛出异常



 */